package rta.datos.conexion
{
	import flash.data.SQLConnection;
	import flash.data.SQLResult;
	import flash.data.SQLStatement;
	import flash.events.SQLErrorEvent;
	import flash.events.SQLEvent;
	
	import mx.collections.ArrayCollection;
	
	import rta.datos.to.Sonido;

	public class SonidoDB
	{
		private var conn:SQLConnection;
		private var statement:SQLStatement;
		private var _callback:Function;
		
		public function SonidoDB(conn:SQLConnection)
		{
			this.conn = conn;
			statement = new SQLStatement();
			statement.itemClass = Sonido;
			statement.sqlConnection = conn;
		}
		
		public function set callback(value:Function):void
		{
			_callback = value;
		}
		//-------------------------------------------------
		
		
		//--OBTENER SONIDOS-----------
		public function obtenerSonidos(idCarpeta:String):void{					
			statement.text = "SELECT id_sonido id, " +
							 "nombre nombre, " +
							 "ruta_sonido rutaSonido " +
							 "FROM sonido " +
							 "WHERE id_carpeta = @idCarpeta";
			statement.parameters["@idCarpeta"] = idCarpeta;
			statement.addEventListener(SQLEvent.RESULT, resultObtenerSonidos);
			statement.execute();
		}
		
		private function resultObtenerSonidos(event:SQLEvent):void{
			statement.removeEventListener(SQLEvent.RESULT, resultObtenerSonidos);
			statement.clearParameters();
			var result:SQLResult = statement.getResult();
			if (result != null && this._callback != null) {  
				this._callback(new ArrayCollection(result.data));
			}
		}
		//-------------------------------------
	}
}